//
//  UINavigationSubController.m
//  RTUApp
//
//  Created by 陈德军 on 2017/5/18.
//  Copyright © 2017年 陈德军. All rights reserved.
//

#import "UINavigationSubController.h"

@interface UINavigationSubController ()<UIGestureRecognizerDelegate>

@end

@implementation UINavigationSubController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationBar.translucent = NO;
    
    [self.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName, [UIFont systemFontOfSize:16.0f], NSFontAttributeName, nil]];
    
    self.navigationBar.barTintColor = KK_MailColor;
    if( ([[[UIDevice currentDevice] systemVersion] doubleValue]>=7.0))
    {
        self.edgesForExtendedLayout = UIRectEdgeNone;//视图控制器，四条边不指定
        self.extendedLayoutIncludesOpaqueBars = NO;//不透明的操作栏
        self.modalPresentationCapturesStatusBarAppearance = NO;
        [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@""]
                                          forBarPosition:UIBarPositionTop
                                              barMetrics:UIBarMetricsDefault];
    }
    else
    {
        [self.navigationBar setBackgroundImage:[UIImage imageNamed:@""]
                                 forBarMetrics:UIBarMetricsDefault];
    }
    
    //右滑返回手势自定义导航栏返回按钮右滑返回手势失效的解决
    self.interactivePopGestureRecognizer.delegate = self;
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


+ (void)initialize {
    
    UINavigationBar *bar = [UINavigationBar appearanceWhenContainedInInstancesOfClasses:@[[self class]]];
    [[UIApplication sharedApplication] setStatusBarStyle:(UIStatusBarStyleLightContent)];

    //    // 设置导航条返回文字
//    [bar setTintColor:[UIColor yellowColor]];
    //    // 获取导航条按钮的标识
    //    UIBarButtonItem *item = [UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[self class]]];
    //    // 修改返回按钮标题的位置
    //    [item setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -1000) forBarMetrics:UIBarMetricsDefault];
    
    //设置导航条的背景图片
    //    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"icon_next"] forBarMetrics:(UIBarMetricsDefault)];
    
    
    //设置导航条透明方法
//    [bar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    
    //在iOS7之后，系统默认会给导航控制器中所有的UIScrollVIew的顶部添加额外的滚动区域64，消除方法
}


- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
    if (self.childViewControllers.count > 0) {
        UIButton *back = [UIButton new];
        [back setTitle:@"返回" forState:(UIControlStateNormal)];
        [back setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
        [back setTitleColor:[UIColor whiteColor] forState:(UIControlStateHighlighted)];
//        [back setImage:[UIImage imageNamed:@"icon_next"] forState:(UIControlStateNormal)];
//        [back setImage:[UIImage imageNamed:@"icon_next"] forState:(UIControlStateHighlighted)];
        [back addTarget:self action:@selector(backClick) forControlEvents:(UIControlEventTouchUpInside)];
        back.frame = CGRectMake(0, 0, 70, 30);
        back.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        back.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
        back.imageEdgeInsets = UIEdgeInsetsMake(0,-20, 0, 0);
        
        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:back];
    }
    [super pushViewController:viewController animated:animated];
}

- (void)backClick{
    [super popViewControllerAnimated:YES];
}


//当当前控制器是跟控制器时，就不接受触摸事件了，
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    return self.childViewControllers.count > 1;
}

@end
